//
//  HREvaluationCell.m
//  haierapp
//
//  Created by gaotangzhang on 14-9-3.
//  Copyright (c) 2014年 haier. All rights reserved.
//

#import "HREvaluationCell.h"

@interface HREvaluationCell ()
@property (weak, nonatomic) IBOutlet EDStarRating *myStar;
@property (weak, nonatomic) IBOutlet UIView *bgView;
@property (weak, nonatomic) IBOutlet UITextField *textField;
@property (weak, nonatomic) IBOutlet UIImageView *productImgView;
@property (weak, nonatomic) IBOutlet UILabel *productNameLabel;
@property (weak, nonatomic) IBOutlet UILabel *productDetailLabel;
@property (weak, nonatomic) IBOutlet UILabel *productParamLabel;

@property (weak, nonatomic) IBOutlet UILabel *scorePromptLabel;


@end

@implementation HREvaluationCell

- (void)awakeFromNib
{
    // Initialization code
}

-(id)init{
    
    self=[[[NSBundle mainBundle]loadNibNamed:@"HREvaluationCell" owner:self options:nil] objectAtIndex:0];
    
    [_myStar setBackgroundColor:[UIColor clearColor]];
    _myStar.delegate=self;
    
    _myStar.starImage=[UIImage imageNamed:@"star_ico_d"];
    _myStar.starHighlightedImage=[UIImage imageNamed:@"star_ico"];
    
    _myStar.maxRating = 5.0;
    _myStar.editable=YES;
    _myStar.rating= 0.0f;
    _myStar.displayMode=EDStarRatingDisplayFull;
    
    //设置圆角半径
    self.bgView.layer.cornerRadius=2.0f;
    
    //设置textField，的paddingLeft
    UIView *view=[[UIView alloc]initWithFrame:CGRectMake(0, 0, 10, 10)];

    _textField.leftView=view;
    _textField.leftViewMode=UITextFieldViewModeAlways;
    _textField.delegate=self;
    
    return self;
}


//设置分数
-(void)setRating:(CGFloat)rating{
    _myStar.rating=rating;
}

//填充数据
-(void)configWithData:(id)data{
    HROrderProductInfo *productInfo=(HROrderProductInfo *)data;
    
    NSURL *imageUrl=[NSURL URLWithString:productInfo.productLinker];
    
    [_productImgView setImageWithURL:imageUrl placeholderImage:[UIImage imageNamed:IMAGENAME_PLACE_HODLE]];
    
    _productNameLabel.text=productInfo.productTitle;
    
    NSString *param=@"";
    for (HRProSpeInfo *info in productInfo.productClassify) {
        param=[NSString stringWithFormat:@"%@%@:%@;",param,info.name,info.value];
    }
    _productParamLabel.text=param;
    
    _productDetailLabel.text=@"";
    
    if (_myStar.rating==0) {
        _scorePromptLabel.text=@"";
    }else{
        _scorePromptLabel.text=[NSString stringWithFormat:@"(已评分%.1f分)",_myStar.rating];
    }
}

#pragma mark - UITextField Delegate Methods

- (void)textFieldDidEndEditing:(UITextField *)textField{
    if ([_delegate respondsToSelector:@selector(evaluateProductContent:andIndex:)]) {
        [_delegate evaluateProductContent:textField.text andIndex:_index];
    }
}


- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    
    [textField resignFirstResponder];
    return YES;
}

#pragma mark - EDStarRating Delegate Methods
-(void)starsSelectionChanged:(EDStarRating*)control rating:(float)rating{
    
    NSLog(@"tarts selection changed");
    
     _scorePromptLabel.text=[NSString stringWithFormat:@"(已评分%.1f分)",_myStar.rating];
    
    if ([_delegate respondsToSelector:@selector(evaluateProductScore:andIndex:)]) {
        [_delegate evaluateProductScore:rating andIndex:_index];
    }
    
}

@end
